local life $life

local ave_panels $ave_panels

local r $r
local deprate $deprate

local KW_per_panel=$KW_per_panel
* https://news.energysage.com/how-many-solar-panels-do-i-need/
*pause off


use  "../Data/SeverinMC/output", clear // private marginal cost for borenstein bushnell marginal price  cents per kwh

collapse (mean) pmc [aw=res_sales], by(state)

replace pmc=pmc/100 // convert to dollars per kwh

save "../Data/CleanedData/StateMC", replace



clear

insheet using `"../Data/CleanedData/state-install-price.csv"'



save "../Data/CleanedData/state-install-price", replace





clear all

import excel `"../Data/Solar_Incentives_Data_no_severin_data.xlsx"', sheet("Sheet1")  firstrow case(lower) cellrange(A1:BA50)

tab state if netmetering==0

	
	
foreach var in rebateperkwh fixedrebate rebateperkw rebateperkwh statetaxcredit srecavgprice statemaxtaxcredit salestaxexemption0or1 propertytaxexemption0or1 {
	replace `var'=0 if `var'==.
}




gen PV=0 // present value

forvalues i=0/`life' {

	replace PV=PV+`deprate'^`i'*1/(`r')^`i'
	
}




replace rebateperkwh=0 if state=="MN" // not a state program in DSIRE
sum rebateperkwh
replace rebateperkw=1500 if state=="OR" // not coded in original dataset


// deal with states with upper limits on rebate per kw
gen sunit=rebateperkw*`KW_per_panel'+fixedrebate/`ave_panels' //  maryland has a fixed rebate of 1000 per system

* some states have caps on these unit subsidies
gen sunit_cap=1000000000000000000

replace sunit_cap=totalofperkwhrebates if  state=="KY" | state=="OR" | state=="WI"

replace sunit_cap=10000 if state=="LA"
replace sunit_cap=1375 if state=="NH"

** massachusetts SRECs only for first ten years of life

gen PV10=0 // present value

forvalues i=0/10 {

	replace PV10=PV10+1/(`r')^`i'
	
}

sum srecavgprice if state=="MA"
replace srecavgprice=srecavgprice*PV10/PV if state=="MA"
sum srecavgprice if state=="MA"



gen PV15=0 // present value

forvalues i=0/15 {

	replace PV15=PV15+1/(`r')^`i'
	
}

replace srecavgprice=srecavgprice*PV15/PV if state=="NJ"



gen skwh=rebateperkwh+srecavgprice

gen scost=federaltaxcredit+statetaxcredit+ ///
avgsalestaxratestatelocal*salestaxexemption0or1 + ///
propertytaxrateavg*propertytaxexemption0or1*PV

gen smeter=0 // (avgretailrate-cost)*smeter

gen price=avgretailrate 

replace price=price*236.7/237 // convert to 2014 price (starts with 2015)



merge 1:1 state using  ../Data/CleanedData/state-install-price.dta

sort state_fips

keep if _merge==3 // drop DC, alaska and Hawaii



order state sunit skwh scost smeter price estimate_intercept estimate_panels // already in 2014 dollars

gen price_sale=price

drop _merge



merge 1:1 state using  "../Data/CleanedData/StateMC"

keep if _merge==3 // drop DC

gen MC=pmc

scatter MC price

graph export StataGraphs/priceMCCorr.png, as(png) replace

replace price_sale=MC if netmetering==0 



sort state_fips

foreach var in sunit skwh scost smeter price price_sale estimate_intercept estimate_panels statemaxtaxcredit statetaxcredit sunit_cap netmetering {
	outsheet `var' using ../Data/CleanedData/`var'.csv, replace nonames
}



drop _merge

save ../Data/CleanedData/StatePrices, replace

tab state if netmetering==0


